package net.cgsoft.widget.https.okhttp;

import android.content.Context;

import net.cgsoft.widget.https.DownCallBack;
import net.cgsoft.widget.utils.Util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Youga on 2016/1/9.
 */
public class FileRequest extends BaseRequest {


    public FileRequest(Context context) {
        super(context);
    }

    @Override
    protected void appendHeader(FormBody.Builder formBuilder) {

    }

    @Override
    protected void appendPageHeader(FormBody.Builder formBuilder) {

    }

    @Override
    protected void sendAlertCallback(Context context, String message) {

    }

    public void downApp(String url, final DownCallBack callBack) {
        final String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
        Request request = new Request.Builder().url(url).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            public int progress;
            private boolean publish;

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                mDelivery.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.downStart();
                    }
                });
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        publish = !publish;
                    }
                }, 0, 500);
                final File apkFile = new File(Util.getRootDir(), fileName);
                if (apkFile.exists()) apkFile.delete();
                InputStream stream = response.body().byteStream();
                final long length = response.body().contentLength();
                Double doubleSize = ((double) length) / 1024 / 1024;
                final String size = doubleSize.toString().length() > 4 ? doubleSize.toString().substring(0, 4) : doubleSize.toString();
                OutputStream out = new FileOutputStream(apkFile);
                int len;
                byte[] buf = new byte[(int) (length / 100)];
                while ((len = stream.read(buf)) != -1) {
                    out.write(buf, 0, len);
                    progress += len;
                    if (publish) {
                        publish = false;
                        mDelivery.post(new Runnable() {
                            @Override
                            public void run() {
                                callBack.downIng(progress * 100 / length, size);
                            }
                        });
                    }
                }
                mDelivery.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.downFinish(apkFile);
                    }
                });
                timer.cancel();
                out.close();
                stream.close();
            }
        });
    }

}
